$(function() {
	initRegist();
//	if("undefined" !== typeof localStorage.userId) {
//		loginRuleSubmit.ruleForm.userId = localStorage.userId;
//		localStorage.removeItem(localStorage.userId);
//		loginRuleSubmit.remember = true;
//	} else {
//		loginRuleSubmit.remember = false;
//	}
	$("body").keydown(function(e) {
		if(event.keyCode === 13) {
			loginRuleSubmit.submitForm("ruleForm");
		}
	});
});

//规则校验，成功回调
var loginRuleSubmit = new Vue({
	el: '#login',
	data: function() {
		var self = this;
		return {
//			remember: false,
			ruleForm: {
				userAccount: '',
				userPasswod: ''
			},
			isSubmit: false,
			rules: {
				userAccount: [{
					required: true,
					message: '请输入用户名称',
					trigger: 'blur'
				}],
				userPasswod: [{
					required: true,
					trigger: 'blur',
					validator: function(rule, value, callback){
						if (value === '') {
				          callback(new Error('请输入密码'));
				        } else{
				        	if(self.isSubmit){
				        		self.isSubmit = false;
				        		callback(new Error(self.rspMsg));
				        	}else{
				        		callback();
				        	}
				        }
					}
				}],
			}
		};
	},
	methods: {
		submitForm: function(formName) {
			var self = this;
			this.$refs[formName].validate(function(valid){
				if(valid){
					$.ajax({
						url: "/login",
						method: "POST",
						async: true,
						data: {
							"userAccount": loginRuleSubmit.ruleForm.userAccount, //用户名
							"password": loginRuleSubmit.ruleForm.userPasswod //密码
						},
						dataType: "json",
						success: function(data) {
							console.log(1)
							self.isSubmit = true;
							if(data.rspCode === '000000') {
								$(".error").html('');
								var resInfo = data.resultData.userInfo;
								sessionStorage.setItem("loginUrl", window.location.href);
								sessionStorage.setItem("username", loginRuleSubmit.ruleForm.userAccount); 
//								sessionStorage.setItem("Jurisdiction", loginRuleSubmit.ruleForm.userId); 
								//保存用户名密码
//								if(loginRuleSubmit.remember == true) {
//									localStorage.setItem("userId", loginRuleSubmit.ruleForm.userId); //
//									localStorage.setItem("userRemember", true);
//								} else {
//									localStorage.removeItem("userId");
//									localStorage.setItem("userRemember", false);
//								}
								sessionStorage.setItem("token", data.resultData.token);
								$.fakeLoadershow();
								console.log("获取token： \n" + sessionStorage.token);
								location.href = "/index";
							} else {
								self.rspMsg = data.rspMsg;
								self.$refs["ruleForm"].validateField("userPasswod");
								/*$(".error").html(data.rspMsg)*/
								return false;
							}
						}
					});
				}else{
					return false;
				}
			});
		}
	}
});

function initRegist() {
	// 自定义相关样式信息
	var windowH = $(window).height(),
		registH = $(".regist_up").height(),
		trueH = (windowH - registH) / 2;
	$(".regist").css("max-height", windowH);
	$(".regist_up").css("margin-top", trueH);
	$(".regist_box").show();
}